package com.ms.module.wechat.clear.fragment.voice;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ms.module.wechat.clear.R;
import com.ms.module.wechat.clear.fragment.adapter.node.FileChildNode;
import com.ms.module.wechat.clear.utils.ByteSizeToStringUnitUtils;
import com.ms.module.wechat.clear.utils.MediaPlayerUtils;
import com.ms.module.wechat.clear.utils.OpenFileUtils;
import com.ms.module.wechat.clear.utils.ToastUtils;
import com.reinhard.wcvcodec.WcvCodec;


import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class VoiceChildProvider extends BaseNodeProvider {
    @Override
    public int getItemViewType() {
        return 2;
    }

    @Override
    public int getLayoutId() {
        return R.layout.provider_details_voice_child;
    }

    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {

        TextView textViewName = baseViewHolder.findView(R.id.textViewName);
        TextView textViewSize = baseViewHolder.findView(R.id.textViewSize);
        ImageView imageViewCheck = baseViewHolder.findView(R.id.imageViewCheck);
        if (baseNode instanceof FileChildNode) {
            FileChildNode voiceChildNode = (FileChildNode) baseNode;
            File file = new File(voiceChildNode.getPath());
            textViewName.setText(file.getName());
            textViewSize.setText(ByteSizeToStringUnitUtils.byteToStringUnit(file.length()));
            imageViewCheck.setSelected(voiceChildNode.isCheck());


            imageViewCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (voiceChildNode.isCheck()) {
                        imageViewCheck.setSelected(false);
                        voiceChildNode.setCheck(false);
                    } else {
                        imageViewCheck.setSelected(true);
                        voiceChildNode.setCheck(true);
                    }

                    getAdapter().notifyDataSetChanged();

                    if (VoiceFragment.getInstance() != null) {
                        VoiceFragment.getInstance().updateSelectAll();
                    }
                }
            });


            baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Observable.create(new ObservableOnSubscribe<Boolean>() {
                        @Override
                        public void subscribe(@NonNull ObservableEmitter<Boolean> emitter) throws Exception {


                            File fileDir = new File("/sdcard/Android/data/" + context.getPackageName() + "/wechatclear/");

                            if (!fileDir.exists()) {
                                fileDir.mkdirs();
                            }

                            File filePcm = new File("/sdcard/Android/data/" + context.getPackageName() + "/wechatclear/" + file.getName() + "out.pcm");
                            File fileMp4 = new File("/sdcard/Android/data/" + context.getPackageName() + "/wechatclear/" + file.getName() + "out.mp3");

                            if (filePcm.exists()) {
                                filePcm.delete();
                            }

                            if (fileMp4.exists()) {
                                fileMp4.delete();
                            }

                            boolean b = WcvCodec.decode(voiceChildNode.getPath(), filePcm.getPath(), fileMp4.getPath()) == 0;
                            emitter.onNext(b);

                            if (b) {
                                MediaPlayerUtils.play("/sdcard/Android/data/" + context.getPackageName() + "/wechatclear/" + file.getName() + "out.mp3");
                            }

                        }
                    }).subscribeOn(Schedulers.io())
                            .subscribeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Observer<Boolean>() {
                                @Override
                                public void onSubscribe(@NonNull Disposable d) {

                                }

                                @Override
                                public void onNext(@NonNull Boolean aBoolean) {
                                    if (aBoolean) {


                                        //OpenFileUtils.openMp3(context, "/sdcard/Android/data/" + context.getPackageName() + "/" + file.getName() + "out.mp3");


                                    } else {
                                        ToastUtils.show(context, "编码失败");
                                    }
                                }

                                @Override
                                public void onError(@NonNull Throwable e) {

                                }

                                @Override
                                public void onComplete() {


                                }
                            });


                }
            });
        }
    }

    private static final String TAG = "VoiceChildProvider";
}
